package cn.itrip.biz.service;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripHotelRoom;
import cn.itrip.beans.pojo.ItripHotelTempStore;
import cn.itrip.beans.vo.*;
import cn.itrip.biz.exception.AddOrderException;
import cn.itrip.dao.ItripHotelOrderMapper;
import cn.itrip.dao.ItripHotelRoomMapper;
import cn.itrip.dao.ItripHotelTempStoreMapper;
import cn.itrip.utlis.ConfigManager;
import cn.itrip.utlis.Constants;
import cn.itrip.utlis.DateUtils;
import cn.itrip.utlis.RandomUtils;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description :
 * Created by Li on 2020-12-28 10:51:12
 */
@Service
public class HotelOrderService {
    @Autowired
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;
    @Autowired
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Autowired
    private ItripHotelRoomMapper itripHotelRoomMapper;

    public List<StoreVO> getpreorderinfo(GetPreorderInfoVO params) {
            params.setStartTime(DateUtils.parse(params.getCheckOutDate()));
            params.setEndTime(DateUtils.parse(params.getCheckOutDate()));
            itripHotelTempStoreMapper.flushStore(params);
            return itripHotelTempStoreMapper.queryRoomStore(params);
    }

    public void addhotelorder(ItripHotelOrder order) throws AddOrderException{
        //计算天数
        int days = Days.daysBetween(LocalDate.fromDateFields(order.getCheckInDate()),
                LocalDate.fromDateFields(order.getCheckOutDate())).getDays();
        if(days < 1) {
            throw new AddOrderException("订房天数不能小于1:" + days);
        }
        order.setBookingDays(days);
        //设置订单编号
        String hostId = ConfigManager.getInstance().getValue("hostId");
        String orderNo = hostId + DateUtils.format(new Date()) + RandomUtils.random(6);
        order.setOrderNo(orderNo);
        //设置订单状态
        order.setOrderStatus(Constants.ORDER_STATUS_WAIT_PAY);
        //计算支付金额
        Integer count = order.getCount();
        ItripHotelRoom hotelRoom = itripHotelRoomMapper.selectByPrimaryKey(order.getRoomId());
        BigDecimal amount = hotelRoom.getRoomPrice().multiply(new BigDecimal(days * count));
        order.setPayAmount(amount);
        if(amount.compareTo(new BigDecimal(0)) != 1){
            throw new AddOrderException("订单支付金额异常:" + amount);
        }
        itripHotelOrderMapper.insert(order);
    }

    public Long getMaxId(){
        return itripHotelOrderMapper.selectMaxId();
    }

    public ItripHotelOrder queryOrderById(Long id) {
        return itripHotelOrderMapper.selectByPrimaryKey(id);
    }

    public GetPersonalOrderListReturn selectByVO(ShowOrderVO showOrderVO, Long userId) {
        GetPersonalOrderListReturn listReturn = new GetPersonalOrderListReturn();
        listReturn.setPageCount(29);
        listReturn.setPageSize(showOrderVO.getPageSize());
        listReturn.setTotal(itripHotelOrderMapper.count(userId));
        listReturn.setCurPage(showOrderVO.getPageNo());
        listReturn.setBeginPos((listReturn.getCurPage()-1)*listReturn.getPageSize());
        listReturn.setRows(itripHotelOrderMapper.selectByUserId(userId, (showOrderVO.getPageNo()-1)*showOrderVO.getPageSize(),showOrderVO.getPageSize()));
        return listReturn;
    }
}
